#pragma once

#include<assert.h>

template<class K,class V>
struct TreeNode
{
	TreeNode<K,V>* _left;
	TreeNode<K,V>* _right;
	TreeNode<K,V>* _parent;
	pair<K, V> _kv;

	int _bf;

	TreeNode(const pair<K,V> kv)
		:_left(nullptr),_right(nullptr),_parent(nullptr),_kv(kv),_bf(0)
	{}
};

template<class K,class V>
class AVLTree
{
	typedef TreeNode<K, V> Node;
public:
	bool insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			return true;
		}

		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (kv.first < cur->_kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if(kv.first > cur->_kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				return false;
			}
		}

		cur = new Node(kv);
		if (kv.first < parent->_kv.first) parent->_left = cur;
		else parent->_right = cur;
		cur->_parent = parent;

		while (parent)
		{
			if (cur == parent->_left) parent->_bf--;
			else parent->_bf++;

			if (parent->_bf == 0)
			{
				break;
			}
			else if (parent->_bf == 1 || parent->_bf == -1)
			{
				cur = parent;
				parent = cur->_parent;
			}
			else if (parent->_bf == 2 || parent->_bf == -2)
			{
				if (parent->_bf == 2 && cur->_bf == 1)
				{
					RotateL(parent);
				}
				else if (parent->_bf == -2 && cur->_bf == -1)

				{
					RotateR(parent);
				}
				else if (parent->_bf == 2 && cur->_bf == -1)
				{
					RotateRL(parent);
				}
				else if (parent->_bf == -2 && cur->_bf == 1)
				{
					RotateLR(parent);
				}
				else
				{
					assert(false);
				}

				break;
			}
			else
			{
				assert(false);
			}
		}
		return true;
	}

	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		
		parent->_left = subLR;
		if(subLR) subLR->_parent = parent;

		Node* ppnode = parent->_parent;
		subL->_right = parent;
		parent->_parent = subL;

		if (parent == _root)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (parent == ppnode->_left)
			{
				ppnode->_left = subL;
			}
			else
			{
				ppnode->_right = subL;
			}
			subL->_parent = ppnode;
		}

		parent->_bf = subL->_bf = 0;
	}
	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;

		if (subRL) subRL->_parent = parent;

		Node* ppnode = parent->_parent;
		subR->_left = parent;
		parent->_parent = subR;

		if (parent == _root)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (parent == ppnode->_left)
			{
				ppnode->_left = subR;
			}
			else
			{
				ppnode->_right = subR;
			}

			subR->_parent = ppnode;
		}
		parent->_bf = subR->_bf = 0;
	}
	void RotateLR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		
		int bf = subLR->_bf;
		
		RotateL(subL);
		RotateR(parent);
		
		if (bf == 0)
		{
			parent->_bf = 0;
			subL->_bf = 0;
			subLR->_bf = 0;
		}
		else if (bf == 1)
		{
			parent->_bf = 0;
			subL->_bf = -1;
			subLR->_bf = 0;
		}
		else if (bf == -1)
		{
			parent->_bf = 1;
			subL->_bf = 0;
			subLR->_bf = 0;
		}
	}
	void RotateRL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		int bf = subRL->_bf;
		RotateR(subR);
		RotateL(parent);

		if (bf == 0)
		{
			parent->_bf = 0;
			subR->_bf = 0;
			subRL->_bf = 0;
		}
		else if (bf == -1)
		{
			parent->_bf = 0;
			subR->_bf = 1;
			subRL->_bf = 0;
		}
		else if (bf == 1)
		{
			subRL->_bf = 0;
			parent->_bf = -1;
			subR->_bf = 0;
		}
	}
	void inorder()
	{
		_inorder(_root);
	}
	bool isbalance()
	{
		return _isbalance(_root);
	}
	int height()
	{
		return _height(_root);
	}
	int size()
	{
		return _size(_root);
	}
private:
	int _size(Node* root)
	{
		return root == nullptr ? 0 : _size(root->_left) + _size(root->_right) + 1;
	}
	void _inorder(Node* root)
	{
		if (root == nullptr) return;
		_inorder(root->_left);
		cout << root->_kv.first << "->" << root->_kv.second << endl;
		_inorder(root->_right);
	}
	int _height(Node* root)
	{
		if (root == nullptr) return 0;
		int left = _height(root->_left);
		int right = _height(root->_right);
		return max(left, right) + 1;
	}
	bool _isbalance(Node* root)
	{
		if (root == nullptr) return true;
		int left = _height(root->_left);
		int right = _height(root->_right);

		if (abs(left - right) >= 2) return false;
		if (right - left != root->_bf) return false;

		return _isbalance(root->_left)
			&& _isbalance(root->_right);
	}
private:
	Node* _root = nullptr;
};


void test_avltree1()
{
	AVLTree<int, int> t;
	//int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16,14};
	int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
	for (auto e : a)
	{
		t.insert({ e,e });
	}
	t.inorder();

	cout << t.isbalance() << endl;
	cout << t.size() << endl;
	cout << t.height() << endl;
}